package com.zlx.mall.basic.datasource.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zlx.mall.basic.common.exception.IllegalBizArgumentException;
import com.zlx.mall.basic.common.model.BaseObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

/**
 * @author qingke
 **/
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class PrimaryKeyBaseRequest extends BaseObject
        implements java.io.Serializable {

    /**
     * PK
     */
    @Nullable
    protected Long id;

    @JsonIgnore
    public boolean isSave() {
        return !isUpdate();
    }

    @JsonIgnore
    public boolean isUpdate() {
        return this.id != null;
    }

    @JsonIgnore
    public Long getNonNullId() {
        return Optional
                .ofNullable(id)
                .orElseThrow(()->new IllegalBizArgumentException("ID must not be null"));
    }

}
